$(function() {

	initTypeTree();
});
// =======================解绑======================
$("#unbindBtn").click(
		function() {
			// 用户
			var bindUserIds = '';
			$(".bindUserSelect").each(function() {
				if ($(this).prop("checked") == true) {
					bindUserIds += $(this).attr("ownId") + ",";
				}
			});
			var typeIds = $("#typeId").val();
			if (typeIds == '') {
				message("请选择要解除绑定的跟节点");
				return false;
			}
			var userIds = bindUserIds;
			if ('' == userIds) {
				message("请选择要解除绑定的用户!");
				return false;
			}

			// 勾选的事件类型
			/*
			 * var treeObj = $.fn.zTree.getZTreeObj("eventTree"); var nodes =
			 * treeObj.getCheckedNodes(true); if (nodes.length == 0) {
			 * message("请选择要解除绑定的事件类型!"); return false; } else { for (var i = 0; i <
			 * nodes.length; i++) { if (nodes[i].id != '0') { typeIds +=
			 * nodes[i].id + ','; } } }
			 */
			if (typeIds == '') {
				message("根节点不在解绑范围!");
				return false;
			}
			typeIds += ",";
			$("#progressbar").show();
			$("#right").mLoading("show");
			$.ajax({
				url : rootPath + '/ui/userEventTypeBind/unbind/' + userIds
						+ "/" + typeIds,
				dataType : 'json',
				type : 'get',
				success : function(result) {
					if (result.code == 1) {
						message(result.data.msg);
						$("#progressbar").hide();
						reloadPage();
					}
					$("#right").mLoading("hide");
				}
			});
		});
// ========================绑定========================
$("#bindBtn").click(
		function() {
			// 用户

			var unbindUserIds = '';
			$(".unbindUserSelect").each(function() {
				if ($(this).prop("checked") == true) {
					unbindUserIds += $(this).attr("ownId") + ",";
				}
			});
			var typeIds = $("#typeId").val();

			var userIds = unbindUserIds;
			if ('' == userIds) {
				message("请选择要绑定的用户!");
				return false;
			}

			// 勾选的事件类型
			/*
			 * var treeObj = $.fn.zTree.getZTreeObj("eventTree"); var nodes =
			 * treeObj.getCheckedNodes(true); if (nodes.length == 0) {
			 * message("请选择要绑定的事件类型!"); return false; } else { for (var i = 0; i <
			 * nodes.length; i++) { if (nodes[i].id != '0') { typeIds +=
			 * nodes[i].id + ','; } } }
			 */
			if (typeIds == '0') {
				message("根节点不在绑定范围!");
				return false;
			}
			// 兼顾 加了一个逗号
			typeIds += ",";
			// return false;
			$("#progressbar").show();
			$("#right").mLoading("show");
			$.ajax({
				url : rootPath + '/ui/userEventTypeBind/bind/' + userIds + "/"
						+ typeIds,
				dataType : 'json',
				type : 'get',
				success : function(result) {
					if (result.code == 1) {
						message(result.data.msg);
						$("#progressbar").hide();
						reloadPage();
					}
					$("#right").mLoading("hide");
				}
			});
		});

function reloadPage() {
	$("#selectAllBind").prop("checked", false);
	$("#selectAllUnbind").prop("checked", false);
	bindUserData.key = $("#typeId").val();
	bindUserData.con = '';
	bindUserTable.draw();
	unbindUserData.key = $("#typeId").val();
	unbindUserData.con = '';
	unbindUserTable.draw();
	var treeObj = $.fn.zTree.getZTreeObj("eventTree");
	treeObj.checkAllNodes(false);
}
// ====================================初始化时间类型树===============================
function initTypeTree() {

	// 初始化 层级树
	var primaryNode = [];
	var url = window.rootPath + '/ui/userEventTypeBind/initTypeTree';
	jQuery.ajax({
		url : url,
		cache : false,
		type : "get",
		data : '',
		dataType : "json",
		contentType : 'application/json;charset=utf-8',// 解决返回数据中的中文乱码问题
		success : function(data) {
			primaryNode.push({
				id : 0,
				name : "事件层级",
				open : "true"
			});
			for (var i = 0; i < data.nodes.length; i++) {
				primaryNode.push(data.nodes[i]);
			}
			$.fn.zTree.init($("#eventTree"), modalSetting, primaryNode);
		}
	});
};
var modalSetting = {
	edit : {
		enable : true,
		showRemoveBtn : false,
		showRenameBtn : false,
	},
	data : {
		simpleData : {
			enable : true,
			idKey : "id",
			pIdKey : "parentId",
			rootParentId : 0
		}
	},
	view : {
		showLine : true
	},

	callback : {
		onClick : showUsers
	}

};
// ====================================已绑定类型的用户信息============================

$("#selectAllBind").click(function() {
	$(".bindUserSelect").prop("checked", $(this).prop("checked"));
});

$(".resetBtn2").click(function() {
	var typeId = $("#typeId").val();
	if ('' == typeId) {
		message("尚未选择类型树!");
		return false;
	}
	$("#search2").val("");
	bindUserData.key = typeId, bindUserData.con = '';
	bindUserTable.draw();
});
$(".searchBtn2").click(function() {
	var typeId = $("#typeId").val();
	if ('' == typeId) {
		message("尚未选择类型树!");
		return false;
	}
	bindUserData.key = typeId, bindUserData.con = $("#search2").val();
	bindUserTable.draw();
});

var bindUserTable;
var bindUserData = {};
function initBindUsers(typeId) {
	// 获取查询内容
	var con = $("#search2").val();
	bindUserData.key = typeId;
	bindUserData.con = con;
	bindUserTable = $('#bindInfo')
			.DataTable(
					{
						"serverSide" : true,
						"ordering" : false,
						"ajax" : {
							"dataType" : "json",
							"type" : "get",
							"cache" : false,
							"url" : window.rootPath
									+ '/ui/userEventTypeBind/showBindUsers',
							"data" : function(d) {
								return $.extend({}, d, bindUserData);
							}
						},
						columns : [
								{
									data : null,
									defaultContent : ""
								},
								{
									data : 'id',
									defaultContent : "",
									render : function(data, type, full, meta) {
										var operate = "<input type='checkbox' class='bindUserSelect' ownId='"
												+ data + "'/>";
										return operate;
									}
								}, {
									data : 'name'
								}, {
									data : 'sex'
								}, {
									data : 'contactType',
								}, {
									data : 'email',
								}, {
									data : 'hierarchy.groupName',
								}

						],
						"drawCallback" : function(oSettings) {
							var k = 0;
							var total = oSettings.aiDisplay.length
									+ oSettings._iDisplayStart;
							for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
								$(
										'td:eq(0)',
										oSettings.aoData[oSettings.aiDisplay[k]].nTr)
										.text(i + 1);
								k++;
							}
							bindUserTable.off('click', 'tr');

						}
					});
	bindUserTable.draw();
};
// ====================================未绑定类型的用户信息============================
$("#selectAllUnbind").click(function() {
	$(".unbindUserSelect").prop("checked", $(this).prop("checked"));
});

$(".resetBtn1").click(function() {
	var typeId = $("#typeId").val();
	if ('' == typeId) {
		message("尚未选择类型树!");
		return false;
	}
	$("#search1").val("");
	unbindUserData.key = typeId, unbindUserData.con = '';
	unbindUserTable.draw();
});
$(".searchBtn1").click(function() {
	var typeId = $("#typeId").val();
	if ('' == typeId) {
		message("尚未选择类型树!");
		return false;
	}
	unbindUserData.key = typeId, unbindUserData.con = $("#search1").val();
	unbindUserTable.draw();
});

var unbindUserTable;
var unbindUserData = {};
function initUnbindUsers(typeId) {
	// 获取查询内容
	var con = $("#search1").val();
	unbindUserData.key = typeId;
	unbindUserData.con = con;
	unbindUserTable = $('#unbindInfo')
			.DataTable(
					{
						"serverSide" : true,
						"ordering" : false,
						"ajax" : {
							"dataType" : "json",
							"type" : "get",
							"cache" : false,
							"url" : window.rootPath
									+ '/ui/userEventTypeBind/showUnbindUsers',
							"data" : function(d) {
								return $.extend({}, d, unbindUserData);
							}
						},
						columns : [
								{
									data : null,
									defaultContent : ""
								},
								{
									data : 'id',
									defaultContent : "",
									render : function(data, type, full, meta) {
										var operate = "<input type='checkbox' class='unbindUserSelect' ownId='"
												+ data + "'/>";
										return operate;
									}
								}, {
									data : 'name'
								}, {
									data : 'sex'
								}, {
									data : 'contactType',
								}, {
									data : 'email',
								}, {
									data : 'hierarchy.groupName',
								}

						],
						"drawCallback" : function(oSettings) {
							var k = 0;
							var total = oSettings.aiDisplay.length
									+ oSettings._iDisplayStart;
							for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
								$(
										'td:eq(0)',
										oSettings.aoData[oSettings.aiDisplay[k]].nTr)
										.text(i + 1);
								k++;
							}
							unbindUserTable.off('click', 'tr');

						}
					});
	unbindUserTable.draw();
};
// ====================================点击显示用户信息===================================
function showUsers(e, treeId, treeNode) {
	var typeId = treeNode.id;
	$("#typeId").val(typeId);
	initUnbindUsers(typeId);
	initBindUsers(typeId);

};
// 删除按钮的设置
function setRemoveBtn(treeId, treeNode) {
	// 判断为顶级节点则不显示删除按钮
	if (treeNode.level == 0)
		return false;
	else
		return true;
}
